package com.com.knowledgepaymentmanagementsystem.controller.login;

import com.com.knowledgepaymentmanagementsystem.pojo.login.LoginBean;
import com.com.knowledgepaymentmanagementsystem.service.user.AdminService;
import com.com.knowledgepaymentmanagementsystem.service.user.UserService;
import com.com.knowledgepaymentmanagementsystem.utils.JwtUtils;
import com.com.knowledgepaymentmanagementsystem.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 张攀
 * @version 1.0
 * @description: TODO 只有管理员登录时下发令牌，只有在特定页面校验令牌，用户登录不下发令牌
 * @date 2025/9/24 上午11:06
 */
@RestController
@CrossOrigin
@Api(tags = "登录页面")
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private AdminService adminService;

    @ApiOperation(value = "登录",httpMethod = "POST",notes = "登录")
    @PostMapping("/login")
    public ResultUtil<HashMap<String,Object>> login(@RequestBody LoginBean loginBean){
        try {
            return new ResultUtil<HashMap<String, Object>>(200,"登录接口成功",userService.login(loginBean));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil<>(500,"登录接口失败");
        }
    }
    @ApiOperation(value = "管理员登录",httpMethod = "POST",notes = "登录")
    @PostMapping("/adminLogin")
    public ResultUtil<Object> masterLogin(@RequestBody LoginBean loginBean){
        HashMap<String, Object> login = adminService.login(loginBean);
        Object o = login.get("msg");
        System.out.println(o);
        if (o == "登录成功") {
            Map<String, Object> claims = new HashMap<>();
            claims.put("account",loginBean.getAccount());
            String jwt = JwtUtils.generateJwt(claims);
            return new ResultUtil<>(200,"登录接口成功",jwt);
        }else
            return new ResultUtil<>(500,"登录失败");
    }
}
